package com.biostime.qdingding.modules.qd.web;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.biostime.qdingding.common.utils.excel.ExportExcel;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.excel.MemberExcel;
import com.biostime.qdingding.modules.excel.OperateExcel;
import com.biostime.qdingding.modules.excel.OrderExcel;
import com.biostime.qdingding.modules.excel.SignedExcel;
import com.biostime.qdingding.modules.qd.entity.CourseOrder;
import com.biostime.qdingding.modules.qd.service.StatExportExcelService;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 统计Controller(报表导出)
 * @author king
 * @version 2015-12-24
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/stat/")
public class StatController extends BaseController {
	
	@Autowired
	private StatExportExcelService exportExcelService;

	@RequestMapping("statmemberexcel")
	public String statMemberExcel(MemberExcel memberExcel,HttpServletRequest request,Model model){
		return "modules/qd/statMemberExcel"; 
	}
	
	@RequestMapping("memberexport")
	public void memberExport(HttpServletResponse response){
		ExportExcel excel = new ExportExcel("会员信息统计报表", MemberExcel.class);
		
		excel.setDataList(exportExcelService.memberExport());
		try {
			excel.write(response, "会员信息统计报表.xlsx");
		} catch (IOException e) {
			logger.error("会员信息报表导出错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("statorderexcel")
	public String statOrderExcel(OrderExcel order, HttpServletRequest request, Model model){
		
		model.addAttribute("order",order);
		
		return "modules/qd/statOrderExcel";
	}
	
	@RequestMapping("orderexport")
	public void orderExport(OrderExcel order, HttpServletResponse response){
		ExportExcel excel = new ExportExcel("耗课统计报表", OrderExcel.class);
		
		excel.setDataList(exportExcelService.orderExport(order));
		try {
			excel.write(response, "耗课统计报表.xlsx");
		} catch (IOException e) {
			logger.error("耗课统计报表导出错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("statoperateexcel")
	public String statOperateExcel(MemberExcel memberExcel,HttpServletRequest request,Model model){
		return "modules/qd/statOperateExcel";
	}
	
	@RequestMapping("operateexport")
	public void operateExport(HttpServletResponse response){
		ExportExcel excel = new ExportExcel("加盟商经营状况表", OperateExcel.class);
		excel.setDataList( exportExcelService.centerExport());
		try {
			excel.write(response, "加盟商经营状况表.xlsx");
		} catch (IOException e) {
			logger.error("加盟商经营状况表导出错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("signedexport")
	public void signedExport(@RequestParam String courseid,HttpServletResponse response){
		List<SignedExcel> list= exportExcelService.signedExport(courseid);
		ExportExcel excel = new ExportExcel(list.get(0).getCourse(), SignedExcel.class);
		excel.setDataList( list);
			try {
				excel.write(response,new StringBuffer(list.get(0).getExcelName()) .append(".xlsx").toString());
			} catch (IOException e) {
				logger.error("签到表导出错误");
				e.printStackTrace();
			}
	}
	
	@RequestMapping("signedzip")
	public String signedZipList(CourseOrder courseOrder,HttpServletResponse response,HttpServletRequest request){
		
		return "modules/qd/signedZipList";
	}
	
	@RequestMapping("signedzipexport")
	public void signedZipExport(CourseOrder courseOrder,HttpServletResponse response,HttpServletRequest request){
		courseOrder.setCenterId(Integer.parseInt(UserUtils.getUser().getCenter().getId()));
		List<String> courseIds = exportExcelService.getCourseIds(courseOrder);
		if(!courseIds.isEmpty())
		signedZipExport(response, courseIds);
	}
	
	public void signedZipExport(HttpServletResponse response,List<String> courseIds){
		List<List<SignedExcel>> excelList;
		excelList = exportExcelService.signedExcelList(courseIds);
		try {
			response.reset();
			response.addHeader("Content-Disposition", new String(("attachment;filename=签到表.rar").getBytes("gb2312"),"ISO8859-1"));
			response.addHeader("Content-Length", "");
			response.setContentType("application/octet-stream");
			ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
			for(List<SignedExcel> tempList :excelList){
				ZipEntry entry = new ZipEntry(tempList.get(0).getExcelName()+".xlsx");
				zipOutputStream.putNextEntry(entry);
				ExportExcel excel = new ExportExcel(tempList.get(0).getCourse(), SignedExcel.class);
				excel.setDataList(tempList);
				ByteArrayOutputStream os = new ByteArrayOutputStream();
					excel.write(os);
					byte[] buffer = os.toByteArray();
					zipOutputStream.write(buffer);
					os.flush();
					os.close();
				}
			zipOutputStream.flush();
			zipOutputStream.close();
		} catch (IOException e) {
				e.printStackTrace();
		}
	}
	
}